package com.armani.service.impl;

import com.armani.entity.Shopcar;
import com.armani.mapper.ImgMapper;
import com.armani.mapper.ShopcarMapper;
import com.armani.mapper.SkuMapper;
import com.armani.model.ShopCarModel;
import com.armani.model.SkuModel;
import com.armani.service.ShopcarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: armani
 * @Package: com.armani.service.impl
 * @ClassName: ShopcarServiceImpl
 * @Description: TODO
 * @Author: Xinyibuzai
 * @Date: 2021/11/29 14:14
 * @Version: 1.0
 **/
@Service
public class ShopcarServiceImpl implements ShopcarService {

    @Autowired
    private ShopcarMapper shopcarMapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private ImgMapper imgMapper;
    @Override
    public List<Shopcar> listSearch(Integer startRow, Integer limit, Integer num) {
        return shopcarMapper.listSearch(startRow, limit, num);
    }

    @Override
    public Integer getCount(Integer num) {
        return shopcarMapper.getCount(num);
    }

    @Override
    public void update(Integer num, Integer userId, Integer skuId, Date insertTime, Integer id) {
        shopcarMapper.update(num, userId, skuId, insertTime, id);
    }

    @Override
    public void insert(Integer num, Integer userId, Integer skuId, Date insertTime) {
        shopcarMapper.insert(num, userId, skuId, insertTime);
    }

    @Override
    public void delete(Integer id) {
        shopcarMapper.delete(id);
    }

    /**
     * 查询用户购物车
     *
     * @param userId 用户id
     * @return 购物车列表
     */
    @Override
    public List<ShopCarModel> selectByUserId(Integer userId) {
        List<ShopCarModel> shopcars = shopcarMapper.selectByUserId(userId);
        for (ShopCarModel shopCar : shopcars) {
            shopCar.setSkuList(skuMapper.searchBySkuId(shopCar.getSkuId()));
            for (SkuModel sku :shopCar.getSkuList()){
                sku.setImgList(imgMapper.searchBySkuId(sku.getId()));
            }
        }
        return shopcars;
    }

    /**
     * 修改购物车商品数量
     *
     * @param params
     * @return 影响行
     */
    @Override
    public Integer editNum(Map<String, Object> params) {
        Integer row =shopcarMapper.editNum(params);
        return row;
    }
}
